package com.zheng.pvz.controller
{
	import com.zheng.pvz.view.HitObjectClass;
	import com.zheng.pvz.view.IHitObject;
	import com.zheng.pvz.view.PlantClass;
	import com.zheng.pvz.view.ShellClass;
	import com.zheng.pvz.view.ZombiesClass;
	
	import flash.display.DisplayObjectContainer;

	/**
	 * 碰撞检测 
	 * @author Administrator
	 * 
	 */
	public class HitTestClass
	{
		public function HitTestClass()
		{
		}
		/**
		 *  
		 * @param mc 需要碰撞检测的容器
		 * 
		 */
		public function checkHit(mc:DisplayObjectContainer):void{
			var num:int=mc.numChildren;
			for(var i:int=0;i<num-1;i++){
				var objA:IHitObject=mc.getChildAt(i) as IHitObject;
				for(var j:int=i+1;j<num;j++ ){
					var objB:IHitObject=mc.getChildAt(j) as IHitObject;
					
					//测试代码==========================
					if(isHit(objA,objB)){
						//确定碰撞后
						if(objA.getType()==ShellClass.TYPE){
							shellHitZombies(ShellClass(objA),ZombiesClass(objB));
							return;
						}
						if(objB.getType()==ShellClass.TYPE){
							shellHitZombies(ShellClass(objB),ZombiesClass(objA));
							return;
						}
					}
					
					
				}
			}
		}
		/**
		 * 碰撞检测 
		 * @return 
		 * 
		 */
		private function isHit(objA:IHitObject,objB:IHitObject):Boolean{
			if(!HitObjectClass(objA).proxy.isHit||!HitObjectClass(objB).proxy.isHit)return false;
			if(objA.getType()==objB.getType())return false;
			if(objA.getType()==(PlantClass.TYPE||ShellClass.TYPE)||objB.getType()==(PlantClass.TYPE||ShellClass.TYPE))return false;
			var dx:int=DisplayObjectContainer(objA).x-DisplayObjectContainer(objB).x;
			var dy:int=DisplayObjectContainer(objA).y-DisplayObjectContainer(objB).y;
			var dist:Number=Math.sqrt(dx*dx+dy*dy);
			var minDist:Number=HitObjectClass(objA).radius+HitObjectClass(objB).radius;
			if(dist<=minDist){
				return true;
			}else{
				return false;
			}
		}
		private function shellHitZombies(shell:ShellClass,zombies:ZombiesClass):void{
			shell.proxy.deductBlood();
			zombies.proxy.deductBlood();
		}
		private function zombiesHitPlant(zombies:ZombiesClass,plant:PlantClass):void{
			
		}
	}
}